package abc.dev.auth.bll;

import abc.base.BaseModel;
import abc.base.IBll;
import abc.dev.auth.client.OnlineAttrClient;
import abc.dev.auth.to.OnlineAttrTo;
import abc.filter.HeadersFilter;
import abc.model.ResponseData;
import abc.enums.ActionType;
import abc.dev.auth.vo.OnlineVo;
import abc.dev.auth.model.OnlineModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import io.swagger.v3.oas.annotations.media.Schema;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;


/**
* <p>
    * 在线账号 (业务逻辑)
    * </p>
*
* @author linlurui
* @since 2022-09-27
*/
@Slf4j
@Schema(name = "OnlineModel", description = "在线账号")
@Component
public class OnlineBll implements IBll<OnlineModel, OnlineVo> {

    @Autowired
    OnlineAttrClient onlineAttrClient;

    @Autowired
    HeadersFilter headersFilter;

    /***
    * 业务调用入口
    */
    @Override
    public ResponseData call(Object data, ActionType actionType) {
        if(actionType == ActionType.OTHER) {
            var model = (BaseModel) data;
            CompletableFuture<?> completableFuture = CompletableFuture.supplyAsync(() -> {
                ResponseData res = onlineAttrClient.doQuery(new OnlineAttrTo() {{
                    setOnlineId(model.getId());
                }});

                return res;
            });
            try {
                return (ResponseData) completableFuture.get();
            } catch (InterruptedException | ExecutionException e) {
                log.error(e.getMessage(), e);
                return error(e.getMessage());
            }
        }
        return null;
    }
}
